package com.quectel.mobile.controller.partybuilding;

import cn.hutool.core.lang.func.LambdaUtil;
import com.quectel.constant.core.partybuilding.PartyBuildingConstants;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.module.partybuildingnotice.dto.PartyBuildingNoticeDto;
import com.quectel.core.module.partybuildingnotice.service.PartyBuildingNoticeService;
import com.quectel.mobile.controller.BaseController;
import com.quectel.mobile.util.SessionHolder;
import com.quectel.mobile.vo.notice.NoticeSimplifyVo;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.common.Page;
import com.quectel.util.common.Request;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 在这里编写说明
 *
 * @author: bob
 * @email: bob.yu@quectel.com
 * @date: 2023-06-13 10:02:00
 */
@RestController
@RequestMapping("partyBuildingNotice")
@Tag(name = "PartyBuildingNoticeController", description = "智慧党建公告/资讯相关api")
public class PartyBuildingNoticeController extends BaseController {

    @DubboReference
    private PartyBuildingNoticeService partyBuildingNoticeService;

    @GetMapping("findById")
    @Operation(summary = "根据条件获取公告详情")
    public Response<PartyBuildingNoticeDto> findById(@Parameter(description = "公告/资讯id") @RequestParam(required = false) Long id) {
        PartyBuildingNoticeDto partyBuildingNoticeDto = partyBuildingNoticeService.selectCacheById(id);
        return Response.<PartyBuildingNoticeDto>ok().wrap(partyBuildingNoticeDto);
    }


    @GetMapping("list")
    @Operation(summary = "移动端党建公告/资讯的分页查询列表")
    public Response<Page<NoticeSimplifyVo>> list(
            @Parameter(description = "大分类 字典值PartyBuildingPublicizeCategory: 1通知公告 2党建资讯") @RequestParam Byte category,
            @Parameter(description = "大分类对应小分类") @RequestParam(required = false) Byte type,
            @Parameter(description = "从1开始的页码数") @RequestParam Integer pageNo,
            @Parameter(description = "每页多少条") @RequestParam Integer pageSize) {

        SessionHolder.checkPartyPerson();

        Request request = Request.configParams(Request.parseParams(httpServletRequest));

        request.getParams().put(LambdaUtil.getFieldName(PartyBuildingNoticeDto::getStatus), SystemConstants.YES);
        request.getParams() .put(PartyBuildingConstants.PARTY_ID_COLLECTION_FLAG,
                SessionHolder.getLoginMobileUserSession().getPartyPerson().getPartyId());

        int totalCount = partyBuildingNoticeService.queryTotal(request.getParams());
        List<PartyBuildingNoticeDto> list = null;
        List<NoticeSimplifyVo> resultList = null;

        if (totalCount > 0) {
            list = partyBuildingNoticeService.queryList(request.getParams());
            resultList = CopyUtils.copyList(list, NoticeSimplifyVo.class);
        } else {
            list = new ArrayList<>();
        }
        Page<NoticeSimplifyVo> page = new Page<>(request.getPageNo(), request.getPageSize(), totalCount, resultList);
        return Response.<Page<NoticeSimplifyVo>>ok().wrap(page);
    }

}
